import 'package:flutter/material.dart';

/// Contains common used methods like showConfirmDialog, showSnackBar.
///
/// Some methods need two kind of parameters, [ScaffoldState] or [BuildContext]. Because you might encounter the below error in [StatefulWidget].
/// Scaffold.of() called with a context that does not contain a Scaffold.
///
/// When it happens it means the [context] you pass cannot be used to show such info. Try to use [state] or follow below suggestions.
///
/// Details:
/// No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of().
/// This usually happens when the context provided is from the same StatefulWidget as that whose
/// build function actually creates the Scaffold widget being sought.
///
/// There are several ways to avoid this problem. The simplest is to use a Builder to get a context that is "under" the Scaffold.
/// For an example of this, please see the documentation for Scaffold.of():
///  https://api.flutter.dev/flutter/material/Scaffold/of.html
///
/// A more efficient solution is to split your build function into several widgets.
/// This introduces a new context from which you can obtain the Scaffold.
/// In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets,
/// and then in these inner widgets you would use Scaffold.of().
///
/// A less elegant but more expedient solution is assign a GlobalKey to the Scaffold,
/// then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
class UserInteraction {
  /// The [BuildContext] or [ScaffoldState] cannot be both null.
  UserInteraction({this.context, this.state})
      : assert(context != null || state != null);

  final BuildContext? context;
  final ScaffoldState? state;

  /// Shows the bottm sheet with [ScaffoldState] or [BuildContext].
  /// With this method the [PersistentBottomSheetController.setState()] does not update the widget status in it.
  /// By now if you need to call [setState] on [PersistentBottomSheetController] consider use getSelectionBottomSheetContent and
  /// construct your bottom sheet instead.
  PersistentBottomSheetController showSelectionBottomSheet(
      List<Widget> selections) {
    if (state != null) {
      return state!.showBottomSheet(
          (context) => getSelectionBottomSheetContent(selections));
    } else {
      return Scaffold.of(context!).showBottomSheet(
          (context) => getSelectionBottomSheetContent(selections));
    }
  }

  /// Gets the widget for bottom sheet provides selections.
  Widget getSelectionBottomSheetContent(List<Widget> selections,
      {bool isHoriz = true}) {
    return Container(
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(
            width: 1.0,
            color: Colors.grey[300]!,
          ),
        ),
      ),
      padding: const EdgeInsets.all(10.0),
      child: isHoriz
          ? Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: selections,
            )
          : Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: selections,
            ),
    );
  }
}
